<?php
namespace Admin\Model;

use Common\Model\CommonModel;

class RechargeModel extends CommonModel {


    public function get_list($where = array(),$order = '',$from =  0,$to = 0){
        $list = M("Recharge")->alias("r")->field("r.*,l.logistics_name")->where($where)
                ->join("bdt_logistics l ON l.id = r.rid")
                ->order($order)
                ->limit($from , $to)
                ->select();

        return $list;
    }

    public function get_card_list($where = array(),$order = '',$from =  0,$to = 0){
        $where['r.model'] = 2;
        $list = M("Recharge")->alias("r")->field("r.*,l.logistics_name,c.number AS card_number,car.car_code,u.user_nicename")->where($where)
            ->join("bdt_card c ON c.id = r.rid","LEFT")
            ->join("bdt_logistics l ON l.id = c.lid","LEFT")
            ->join("bdt_car car ON car.card_id = c.id","LEFT")
            ->join("bdt_users u ON u.id = r.uid","LEFT")
            ->order($order)
            ->limit($from , $to)
            ->select();
        return $list;
    }

    /**
     * 新增充值记录
     * $model = 1 物流公司充值  =2  会员充值
     */
    public function insert_data($insert,$model){
        $rid = $insert['rid'];  //物流公司 或 会员ID
        if(empty($rid)){
            return -1;
        }
        $insert['price'] = round($insert['price'],2);
        $insert['general_price'] = round($insert['general_price'],2);
        $insert['self_price'] =  round($insert['self_price'],2);
        $insert['active_price'] =  round($insert['active_price'],2);
        $insert['create_time'] = time();
        $insert['model'] = $model;
        $insert['uid'] = session('ADMIN_ID');
        $insert['number'] = date("YmdHis").rand(10000,99999);
        $price = round($insert['price']+$insert['general_price']+$insert['self_price'],2);
        if($model == 1){    //物流公司充值
            $logistics =  M("Logistics")->where(array("id"=>$rid))->find();
            if(empty($logistics)){
                return -2;
            }
            $insert['before_balance'] = $logistics['balance'];
            $insert['after_balance'] = round($logistics['balance']+$price,2);
        }elseif($model == 2){   //会员充值
            $logistics =get_logistics_data();
            $insert['lid'] = $logistics['id'];
            $card =  M("Card")->where(array("id"=>$rid))->find();
            if(empty($card)){
                return -2;
            }
            $insert['before_balance'] = $card['balance'];
            $insert['after_balance'] = round($card['balance']+$price,2);
        }


        $result =  M("Recharge")->add($insert);
        if($result){
            if($model == 1){
                //加入账单记录
                D("Logistics/LogisticsBill")->insert_data($logistics['id'],$insert['price'],2);
                if(!empty($insert['general_price'])){
                    D("Logistics/LogisticsBill")->insert_data($logistics['id'],$insert['general_price'],5);
                }
                return true;
            }elseif($model == 2){
                $lid = $logistics['id'];
                M("Card")->where(array("id"=>$card['id']))->save(array("balance"=>round($card['balance']+$price,2)));
                D("Logistics/LogisticsBill")->insert_data($lid,$insert['price'],3);
                //激活充值赠送
                if(!empty($insert['active_price'])){
                    D("Logistics/LogisticsBill")->insert_data($lid,$insert['active_price'],4);
                }
                //卡片充值赠送账单
                $give_price = $insert['self_price'];
                if(!empty($give_price)){
                    D("Logistics/LogisticsBill")->insert_data($lid,$give_price,6);
                }


                return true;
            }
        }else{
            return -3;
        }
    }

}